import * as index from '@/api/index.js'
export default {
	data() {
		return {
			token:uni.getStorageSync('token'),
			menu: uni.getMenuButtonBoundingClientRect(),
			form: {
				title: '',
				con_id: '',
				page: 1
			},
			tabList: [{
				name: '全部',
				num: 0,
				con_id: ''
			}],
			tabIndex: 0,
			banner: [],
			receiveOrderList: [],
			conList: [],
			list: [],
			loginInfo: {
				mobilecode: '',
				code: ''
			}, //登陆参数
			userInfo:{
				mobile:''
			},
			background: '',
		}
	},

	onShow() {
		this.token = uni.getStorageSync('token')
		this.getUserInfo()
		this.getReceiveOrderList()
	},
	onLoad(option) {
		this.getbannerList()
		this.getReceiveOrderList()
		this.getOrderList()
		this.getTabList()
		if(this.token){
			this.haveCoupon()
		}
	},
	onReachBottom() {
		this.form.page += 1
		// this.list = []
		this.getOrderList()
	},
	onPageScroll(e) {
		// console.log(e.scrollTop);
		if (e.scrollTop > 200) {
			this.background = '#C5E0FE'
		} else if (e.scrollTop < 20) {
			this.background = 'transparent'
		} else {
			this.background = `rgba(197,224,254,${(200-e.scrollTop)/200})`
		}
	},
	methods: {
		getUserInfo(){
			if(this.token){
				index.getUserInfo().then(res=>{
					this.userInfo = res.data
				})
			}
		},
		tableChange(item, index) {
			this.tabIndex = index
			if (item.con_id != this.form.con_id) {
				this.form.con_id = item.con_id
				this.form.page = 1
				this.list = []
				this.getOrderList()
			}
		},
		getbannerList() {
			index.getbannerList().then(res => {
				this.banner = res.data
			})
		},
		getReceiveOrderList() {
			index.getReceiveOrderList().then(res => {
				// this.receiveOrderList = res.data
				var list = res.data
				var result = [];
				for (var i = 0; i < list.length; i += 3) {
					result.push(list.slice(i, i + 3));
				}
				this.receiveOrderList = result
			})
		},
		search() {
			this.form.page = 1
			this.list = []
			this.getOrderList()
		},
		//施工类型
		// getConList(){
		// 	this.conList = res.data
		// 	res.data.forEach(item=>{

		// 	})
		// },
		//实施案例
		getTabList() {
			index.getOrderList(this.form).then(res => {

				res.data.con_order_count.forEach(item => {
					this.tabList.push(item)
				})
			})

		},
		getOrderList() {
			index.getOrderList(this.form).then(res => {

				this.tabList[0].num = res.data.count
				this.list.push(...res.data.list.data)
			})
		},
		jump(item) {
			this.$tools.navigate(item)
		},
		haveCoupon() {
			let that = this
			index.haveCoupon().then(res => {
				console.log(res);
				if (res.data.id) {
					uni.showModal({
						title: '提示',
						content: '平台赠送您价值' + res.data.price + '元' + res.data.name,
						confirmText: '领取',
						success: function(resf) {
							if (resf.confirm) {
								console.log(res.data.id);
								that.receiveCoupon(res.data.id)
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});

				}
			})
		},
		receiveCoupon(id) {
			index.receiveCoupon({
				coupon_id: id
			}).then(res => {
				this.$tools.toast(res.msg)
			})
		},
		priview(item, i) {
			uni.previewImage({
				urls: item.images,
				current: i,
				longPressActions: {
					itemList: ['发送给朋友', '保存图片', '收藏'],
					success: function(data) {
						console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
					},
					fail: function(err) {
						console.log(err.errMsg);
					}
				}
			});
		},
		getPhoneNumber(e) {
			let that = this
			// this.getwxCode()
			that.loginInfo.mobilecode = e.detail.code
			uni.login({
				provider: 'weixin',
				onlyAuthorize: true,
				success: function(loginRes) {
					that.loginInfo.code = loginRes.code
					if (that.loginInfo.mobilecode) {
						that.wxLogin()
					}
				}
			});
		},
		wxLogin(){
			let that = this
			index.wxLogin(this.loginInfo).then(res=>{
				uni.setStorage({
					key: 'userInfo',
					data: res.data.userinfo,
					success: function() {
						that.userInfo = res.data.userinfo
						// uni.$emit('company', {
						// 	msg: '页面更新'
						// })
					}
				})
				uni.setStorage({
					key: 'token',
					data: res.data.userinfo.token,
					success: function() {
						that.token = res.data.userinfo.token
						that.getUserInfo()
						uni.showToast({
							icon: 'none',
							title: '登录成功',
							duration: 2000
						});
				
					}
				});
			})
		},
	}
}